﻿using System;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using HslCommunication.Core;
using HslCommunication.Core.IMessage;
using HslCommunication.Core.Net;

namespace HslCommunication.Enthernet
{
	/// <summary>
	/// 同步访问数据的客户端类，用于向服务器请求一些确定的数据信息
	/// </summary>
	/// <remarks>
	/// 详细的使用说明，请参照博客<a href="http://www.cnblogs.com/dathlin/p/7697782.html">http://www.cnblogs.com/dathlin/p/7697782.html</a>
	/// </remarks>
	/// <example>
	/// 此处贴上了Demo项目的服务器配置的示例代码
	/// <code lang="cs" source="TestProject\HslCommunicationDemo\Hsl\FormSimplifyNet.cs" region="FormSimplifyNet" title="FormSimplifyNet示例" />
	/// </example>
	// Token: 0x020000FE RID: 254
	public class NetSimplifyClient : NetworkDoubleBase
	{
		/// <summary>
		/// 实例化一个客户端的对象，用于和服务器通信
		/// </summary>
		/// <param name="ipAddress">服务器的ip地址</param>
		/// <param name="port">服务器的端口号</param>
		// Token: 0x0600153A RID: 5434 RVA: 0x0006E610 File Offset: 0x0006C810
		public NetSimplifyClient(string ipAddress, int port)
		{
			base.ByteTransform = new RegularByteTransform();
			this.IpAddress = ipAddress;
			this.Port = port;
		}

		/// <summary>
		/// 实例化一个客户端的对象，用于和服务器通信
		/// </summary>
		/// <param name="ipAddress">服务器的ip地址</param>
		/// <param name="port">服务器的端口号</param>
		// Token: 0x0600153B RID: 5435 RVA: 0x0006E636 File Offset: 0x0006C836
		public NetSimplifyClient(IPAddress ipAddress, int port)
		{
			base.ByteTransform = new RegularByteTransform();
			this.IpAddress = ipAddress.ToString();
			this.Port = port;
		}

		/// <summary>
		/// 实例化一个客户端对象，需要手动指定Ip地址和端口
		/// </summary>
		// Token: 0x0600153C RID: 5436 RVA: 0x00024933 File Offset: 0x00022B33
		public NetSimplifyClient()
		{
			base.ByteTransform = new RegularByteTransform();
		}

		/// <inheritdoc />
		// Token: 0x0600153D RID: 5437 RVA: 0x0006E661 File Offset: 0x0006C861
		protected override INetMessage GetNewNetMessage()
		{
			return new HslMessage();
		}

		/// <inheritdoc />
		// Token: 0x0600153E RID: 5438 RVA: 0x0006E668 File Offset: 0x0006C868
		protected override OperateResult InitializationOnConnect(Socket socket)
		{
			bool isUseAccountCertificate = this.isUseAccountCertificate;
			OperateResult result;
			if (isUseAccountCertificate)
			{
				result = base.AccountCertificate(socket);
			}
			else
			{
				result = OperateResult.CreateSuccessResult();
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x0600153F RID: 5439 RVA: 0x0006E694 File Offset: 0x0006C894
		[DebuggerStepThrough]
		protected override Task<OperateResult> InitializationOnConnectAsync(Socket socket)
		{
			NetSimplifyClient.<InitializationOnConnectAsync>d__5 <InitializationOnConnectAsync>d__ = new NetSimplifyClient.<InitializationOnConnectAsync>d__5();
			<InitializationOnConnectAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<InitializationOnConnectAsync>d__.<>4__this = this;
			<InitializationOnConnectAsync>d__.socket = socket;
			<InitializationOnConnectAsync>d__.<>1__state = -1;
			<InitializationOnConnectAsync>d__.<>t__builder.Start<NetSimplifyClient.<InitializationOnConnectAsync>d__5>(ref <InitializationOnConnectAsync>d__);
			return <InitializationOnConnectAsync>d__.<>t__builder.Task;
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字符串数据，忽略了自定义消息反馈
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001540 RID: 5440 RVA: 0x0006E6E0 File Offset: 0x0006C8E0
		public OperateResult<string> ReadFromServer(NetHandle customer, string send)
		{
			OperateResult<byte[]> operateResult = this.ReadFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
			bool flag = !operateResult.IsSuccess;
			OperateResult<string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<string>(Encoding.Unicode.GetString(operateResult.Content));
			}
			return result;
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字符串数组，忽略了自定义消息反馈
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001541 RID: 5441 RVA: 0x0006E738 File Offset: 0x0006C938
		public OperateResult<string[]> ReadFromServer(NetHandle customer, string[] send)
		{
			OperateResult<byte[]> operateResult = this.ReadFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
			bool flag = !operateResult.IsSuccess;
			OperateResult<string[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<string[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<string[]>(HslProtocol.UnPackStringArrayFromByte(operateResult.Content));
			}
			return result;
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字节数据
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送的字节内容</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001542 RID: 5442 RVA: 0x0006E78C File Offset: 0x0006C98C
		public OperateResult<byte[]> ReadFromServer(NetHandle customer, byte[] send)
		{
			return this.ReadFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字符串数据，并返回状态信息
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001543 RID: 5443 RVA: 0x0006E7B8 File Offset: 0x0006C9B8
		public OperateResult<NetHandle, string> ReadCustomerFromServer(NetHandle customer, string send)
		{
			OperateResult<NetHandle, byte[]> operateResult = this.ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
			bool flag = !operateResult.IsSuccess;
			OperateResult<NetHandle, string> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<NetHandle, string>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<NetHandle, string>(operateResult.Content1, Encoding.Unicode.GetString(operateResult.Content2));
			}
			return result;
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字符串数据，并返回状态信息
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001544 RID: 5444 RVA: 0x0006E814 File Offset: 0x0006CA14
		public OperateResult<NetHandle, string[]> ReadCustomerFromServer(NetHandle customer, string[] send)
		{
			OperateResult<NetHandle, byte[]> operateResult = this.ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
			bool flag = !operateResult.IsSuccess;
			OperateResult<NetHandle, string[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<NetHandle, string[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<NetHandle, string[]>(operateResult.Content1, HslProtocol.UnPackStringArrayFromByte(operateResult.Content2));
			}
			return result;
		}

		/// <summary>
		/// 客户端向服务器进行请求，请求字符串数据，并返回状态信息
		/// </summary>
		/// <param name="customer">用户的指令头</param>
		/// <param name="send">发送数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001545 RID: 5445 RVA: 0x0006E86C File Offset: 0x0006CA6C
		public OperateResult<NetHandle, byte[]> ReadCustomerFromServer(NetHandle customer, byte[] send)
		{
			return this.ReadCustomerFromServerBase(HslProtocol.CommandBytes(customer, base.Token, send));
		}

		/// <summary>
		/// 需要发送的底层数据
		/// </summary>
		/// <param name="send">需要发送的底层数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001546 RID: 5446 RVA: 0x0006E898 File Offset: 0x0006CA98
		private OperateResult<byte[]> ReadFromServerBase(byte[] send)
		{
			OperateResult<NetHandle, byte[]> operateResult = this.ReadCustomerFromServerBase(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = OperateResult.CreateSuccessResult<byte[]>(operateResult.Content2);
			}
			return result;
		}

		/// <summary>
		/// 需要发送的底层数据
		/// </summary>
		/// <param name="send">需要发送的底层数据</param>
		/// <returns>带返回消息的结果对象</returns>
		// Token: 0x06001547 RID: 5447 RVA: 0x0006E8D4 File Offset: 0x0006CAD4
		private OperateResult<NetHandle, byte[]> ReadCustomerFromServerBase(byte[] send)
		{
			OperateResult<byte[]> operateResult = base.ReadFromCoreServer(send);
			bool flag = !operateResult.IsSuccess;
			OperateResult<NetHandle, byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<NetHandle, byte[]>(operateResult);
			}
			else
			{
				result = HslProtocol.ExtractHslData(operateResult.Content);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.NetSimplifyClient.ReadFromServer(HslCommunication.NetHandle,System.String)" />
		// Token: 0x06001548 RID: 5448 RVA: 0x0006E910 File Offset: 0x0006CB10
		[DebuggerStepThrough]
		public Task<OperateResult<string>> ReadFromServerAsync(NetHandle customer, string send)
		{
			NetSimplifyClient.<ReadFromServerAsync>d__14 <ReadFromServerAsync>d__ = new NetSimplifyClient.<ReadFromServerAsync>d__14();
			<ReadFromServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string>>.Create();
			<ReadFromServerAsync>d__.<>4__this = this;
			<ReadFromServerAsync>d__.customer = customer;
			<ReadFromServerAsync>d__.send = send;
			<ReadFromServerAsync>d__.<>1__state = -1;
			<ReadFromServerAsync>d__.<>t__builder.Start<NetSimplifyClient.<ReadFromServerAsync>d__14>(ref <ReadFromServerAsync>d__);
			return <ReadFromServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.NetSimplifyClient.ReadFromServer(HslCommunication.NetHandle,System.String[])" />
		// Token: 0x06001549 RID: 5449 RVA: 0x0006E964 File Offset: 0x0006CB64
		[DebuggerStepThrough]
		public Task<OperateResult<string[]>> ReadFromServerAsync(NetHandle customer, string[] send)
		{
			NetSimplifyClient.<ReadFromServerAsync>d__15 <ReadFromServerAsync>d__ = new NetSimplifyClient.<ReadFromServerAsync>d__15();
			<ReadFromServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<string[]>>.Create();
			<ReadFromServerAsync>d__.<>4__this = this;
			<ReadFromServerAsync>d__.customer = customer;
			<ReadFromServerAsync>d__.send = send;
			<ReadFromServerAsync>d__.<>1__state = -1;
			<ReadFromServerAsync>d__.<>t__builder.Start<NetSimplifyClient.<ReadFromServerAsync>d__15>(ref <ReadFromServerAsync>d__);
			return <ReadFromServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.NetSimplifyClient.ReadFromServer(HslCommunication.NetHandle,System.Byte[])" />
		// Token: 0x0600154A RID: 5450 RVA: 0x0006E9B8 File Offset: 0x0006CBB8
		[DebuggerStepThrough]
		public Task<OperateResult<byte[]>> ReadFromServerAsync(NetHandle customer, byte[] send)
		{
			NetSimplifyClient.<ReadFromServerAsync>d__16 <ReadFromServerAsync>d__ = new NetSimplifyClient.<ReadFromServerAsync>d__16();
			<ReadFromServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadFromServerAsync>d__.<>4__this = this;
			<ReadFromServerAsync>d__.customer = customer;
			<ReadFromServerAsync>d__.send = send;
			<ReadFromServerAsync>d__.<>1__state = -1;
			<ReadFromServerAsync>d__.<>t__builder.Start<NetSimplifyClient.<ReadFromServerAsync>d__16>(ref <ReadFromServerAsync>d__);
			return <ReadFromServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.NetSimplifyClient.ReadCustomerFromServer(HslCommunication.NetHandle,System.String)" />
		// Token: 0x0600154B RID: 5451 RVA: 0x0006EA0C File Offset: 0x0006CC0C
		[DebuggerStepThrough]
		public Task<OperateResult<NetHandle, string>> ReadCustomerFromServerAsync(NetHandle customer, string send)
		{
			NetSimplifyClient.<ReadCustomerFromServerAsync>d__17 <ReadCustomerFromServerAsync>d__ = new NetSimplifyClient.<ReadCustomerFromServerAsync>d__17();
			<ReadCustomerFromServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<NetHandle, string>>.Create();
			<ReadCustomerFromServerAsync>d__.<>4__this = this;
			<ReadCustomerFromServerAsync>d__.customer = customer;
			<ReadCustomerFromServerAsync>d__.send = send;
			<ReadCustomerFromServerAsync>d__.<>1__state = -1;
			<ReadCustomerFromServerAsync>d__.<>t__builder.Start<NetSimplifyClient.<ReadCustomerFromServerAsync>d__17>(ref <ReadCustomerFromServerAsync>d__);
			return <ReadCustomerFromServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.NetSimplifyClient.ReadCustomerFromServer(HslCommunication.NetHandle,System.String[])" />
		// Token: 0x0600154C RID: 5452 RVA: 0x0006EA60 File Offset: 0x0006CC60
		[DebuggerStepThrough]
		public Task<OperateResult<NetHandle, string[]>> ReadCustomerFromServerAsync(NetHandle customer, string[] send)
		{
			NetSimplifyClient.<ReadCustomerFromServerAsync>d__18 <ReadCustomerFromServerAsync>d__ = new NetSimplifyClient.<ReadCustomerFromServerAsync>d__18();
			<ReadCustomerFromServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<NetHandle, string[]>>.Create();
			<ReadCustomerFromServerAsync>d__.<>4__this = this;
			<ReadCustomerFromServerAsync>d__.customer = customer;
			<ReadCustomerFromServerAsync>d__.send = send;
			<ReadCustomerFromServerAsync>d__.<>1__state = -1;
			<ReadCustomerFromServerAsync>d__.<>t__builder.Start<NetSimplifyClient.<ReadCustomerFromServerAsync>d__18>(ref <ReadCustomerFromServerAsync>d__);
			return <ReadCustomerFromServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.NetSimplifyClient.ReadCustomerFromServer(HslCommunication.NetHandle,System.Byte[])" />
		// Token: 0x0600154D RID: 5453 RVA: 0x0006EAB4 File Offset: 0x0006CCB4
		[DebuggerStepThrough]
		public Task<OperateResult<NetHandle, byte[]>> ReadCustomerFromServerAsync(NetHandle customer, byte[] send)
		{
			NetSimplifyClient.<ReadCustomerFromServerAsync>d__19 <ReadCustomerFromServerAsync>d__ = new NetSimplifyClient.<ReadCustomerFromServerAsync>d__19();
			<ReadCustomerFromServerAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<NetHandle, byte[]>>.Create();
			<ReadCustomerFromServerAsync>d__.<>4__this = this;
			<ReadCustomerFromServerAsync>d__.customer = customer;
			<ReadCustomerFromServerAsync>d__.send = send;
			<ReadCustomerFromServerAsync>d__.<>1__state = -1;
			<ReadCustomerFromServerAsync>d__.<>t__builder.Start<NetSimplifyClient.<ReadCustomerFromServerAsync>d__19>(ref <ReadCustomerFromServerAsync>d__);
			return <ReadCustomerFromServerAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.NetSimplifyClient.ReadFromServerBase(System.Byte[])" />
		// Token: 0x0600154E RID: 5454 RVA: 0x0006EB08 File Offset: 0x0006CD08
		[DebuggerStepThrough]
		private Task<OperateResult<byte[]>> ReadFromServerBaseAsync(byte[] send)
		{
			NetSimplifyClient.<ReadFromServerBaseAsync>d__20 <ReadFromServerBaseAsync>d__ = new NetSimplifyClient.<ReadFromServerBaseAsync>d__20();
			<ReadFromServerBaseAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadFromServerBaseAsync>d__.<>4__this = this;
			<ReadFromServerBaseAsync>d__.send = send;
			<ReadFromServerBaseAsync>d__.<>1__state = -1;
			<ReadFromServerBaseAsync>d__.<>t__builder.Start<NetSimplifyClient.<ReadFromServerBaseAsync>d__20>(ref <ReadFromServerBaseAsync>d__);
			return <ReadFromServerBaseAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Enthernet.NetSimplifyClient.ReadCustomerFromServerBase(System.Byte[])" />
		// Token: 0x0600154F RID: 5455 RVA: 0x0006EB54 File Offset: 0x0006CD54
		[DebuggerStepThrough]
		private Task<OperateResult<NetHandle, byte[]>> ReadCustomerFromServerBaseAsync(byte[] send)
		{
			NetSimplifyClient.<ReadCustomerFromServerBaseAsync>d__21 <ReadCustomerFromServerBaseAsync>d__ = new NetSimplifyClient.<ReadCustomerFromServerBaseAsync>d__21();
			<ReadCustomerFromServerBaseAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<NetHandle, byte[]>>.Create();
			<ReadCustomerFromServerBaseAsync>d__.<>4__this = this;
			<ReadCustomerFromServerBaseAsync>d__.send = send;
			<ReadCustomerFromServerBaseAsync>d__.<>1__state = -1;
			<ReadCustomerFromServerBaseAsync>d__.<>t__builder.Start<NetSimplifyClient.<ReadCustomerFromServerBaseAsync>d__21>(ref <ReadCustomerFromServerBaseAsync>d__);
			return <ReadCustomerFromServerBaseAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06001550 RID: 5456 RVA: 0x0006EB9F File Offset: 0x0006CD9F
		public override string ToString()
		{
			return string.Format("NetSimplifyClient[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
